{% extends "stocksys/base/base_page.html" %}
{% block headscript %}
    <script>
        function delete_mystock(stock_address_id, stock_code) {
            var token = $.cookie('csrftoken');
            $.ajax({
                url: "{% url 'stocksys:add_mystock' %}",
                type: "DELETE",
                data: {'stock_address_id': stock_address_id, 'stock_code': stock_code},
                headers: {'X-CSRFToken': token},
                dataType: "json",
                success: function (data) {
                    if (data['status'] === 200) {
                        $("#" + stock_code).remove();
                        swal({
                            title: "删除成功",
                            text: "成功删除自选",
                            buttonsStyling: false,
                            confirmButtonClass: "btn btn-success",
                            type: "success"
                        }).catch(swal.noop);
                    } else {
                        swal({
                            title: "删除失败",
                            text: "当前自选不存在",
                            buttonsStyling: false,
                            confirmButtonClass: "btn btn-success",
                            type: "error"
                        }).catch(swal.noop);
                    }
                },
                error: function (e) {
                    console.log(e);
                }
            });
        }

        function buy_stock(stock_address_id, stock_code, stock_name, price) {
            var token = $.cookie('csrftoken');
            swal({
                title: '买入',
                html: '<div class="form-group">' +
                    '<p>名称:' + stock_name + '</p>' +
                    '<p>代码:' + stock_code + '</p>' +
                    '<input id="input-price" type="text" value="' + price + '" placeholder="买入价格" class="form-control" />' +
                    '<input id="input-number" type="text" value="100" placeholder="买入数量" class="form-control" />' +
                    '</div>',
                showCancelButton: true,
                confirmButtonClass: 'btn btn-success',
                cancelButtonClass: 'btn btn-danger',
                buttonsStyling: false
            }).then(function (result) {
                let price = $('#input-price').val();
                let number = $('#input-number').val();
                $.ajax({
                    url: "{% url 'stocksys:buy_stock' %}",
                    type: "PUT",
                    data: {
                        'stock_address_id': stock_address_id,
                        'stock_code': stock_code,
                        'price': price,
                        'number': number
                    },
                    headers: {'X-CSRFToken': token},
                    dataType: "json",
                    success: function (data) {
                        if (data['status'] === 200) {
                            swal({
                                title: "成功",
                                text: data['info'],
                                buttonsStyling: false,
                                confirmButtonClass: "btn btn-success",
                                type: "success"
                            }).catch(swal.noop);
                        } else {
                            swal({
                                title: "失败",
                                text: data['info'],
                                buttonsStyling: false,
                                confirmButtonClass: "btn btn-success",
                                type: "error"
                            }).catch(swal.noop);
                        }
                    },
                    error: function (e) {
                        console.log(e);
                    }
                });

            }).catch(swal.noop);

        }

        function sell_stock(stock_address_id, stock_code, stock_name, price) {
            var token = $.cookie('csrftoken');
            swal({
                title: '卖出',
                html: '<div class="form-group">' +
                    '<p>名称:' + stock_name + '</p>' +
                    '<p>代码:' + stock_code + '</p>' +
                    '<input id="input-price" type="text" value="' + price + '" placeholder="卖出价格" class="form-control" />' +
                    '<input id="input-number" type="text" placeholder="卖出数量" class="form-control" />' +
                    '</div>',
                showCancelButton: true,
                confirmButtonClass: 'btn btn-success',
                cancelButtonClass: 'btn btn-danger',
                buttonsStyling: false
            }).then(function (result) {
                let price = $('#input-price').val();
                let number = $('#input-number').val();
                $.ajax({
                    url: "{% url 'stocksys:edit_price' %}",
                    type: "UPDATE",
                    data: {
                        'stock_address_id': stock_address_id,
                        'stock_code': stock_code,
                        'price': price,
                        'number': number
                    },
                    headers: {'X-CSRFToken': token},
                    dataType: "json",
                    success: function (data) {
                        if (data['status'] === 200) {
                            swal({
                                title: "成功",
                                text: data['info'],
                                buttonsStyling: false,
                                confirmButtonClass: "btn btn-success",
                                type: "success"
                            }).catch(swal.noop);
                        } else {
                            swal({
                                title: "失败",
                                text: data['info'],
                                buttonsStyling: false,
                                confirmButtonClass: "btn btn-success",
                                type: "error"
                            }).catch(swal.noop);
                        }
                    },
                    error: function (e) {
                        console.log(e);
                    }
                });

            }).catch(swal.noop);

        }
    </script>
{% endblock %}
{% load staticfiles %}
{% block content %}
    <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="content">
                            <div class="material-datatables">
                                <table id="datatables" class="table table-striped table-no-bordered table-hover">
                                    <thead>
                                    <tr>
                                        <th>股票</th>
                                        <th>代码</th>
                                        <th>当前价格</th>
                                        <th>涨跌额</th>
                                        <th>涨跌幅</th>
                                        <th>成交量</th>
                                        <th>成交额</th>
                                        <th class="disabled-sorting text-center">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {% for stock_info in stock_infos %}
                                        <tr id="{{ stock_info.stock.code }}" class="{{ stock_info.color }}">
                                            <td>
                                                <a href="{% url 'stocksys:stock_info' code=stock_info.stock.code addr=stock_info.stock.exchange_address.id year=now_year %}">{{ stock_info.stock.name }}</a>
                                            </td>
                                            <td>{{ stock_info.stock.code }}</td>
                                            <td>{{ stock_info.current_price| floatformat:3 }}</td>
                                            <td>{{ stock_info.price_chg| floatformat:3 }}</td>
                                            <td>{{ stock_info.price_chg_percent| floatformat:3 }}%</td>
                                            <td>{{ stock_info.volume| floatformat:3 }}</td>
                                            <td>{{ stock_info.money| floatformat:3 }}</td>
                                            <td class="td-actions text-center">
                                                <p id="{% url 'stocksys:delete_mystock' %}"
                                                   onclick="delete_mystock({{ stock_info.stock.exchange_address.id }},'{{ stock_info.stock.code }}')"
                                                   title="删除自选"
                                                   class="btn btn-danger btn-simple btn-xs">
                                                    <i class="ti-close"></i>
                                                </p>
                                                <p id="{% url 'stocksys:buy_stock' %}"
                                                   rel="tooltip" title="预测"
                                                   class="btn btn-success btn-simple btn-xs">
                                                    <a href="{% url 'stocksys:quant_rule_predict' addr=stock_info.stock.exchange_address.id code=stock_info.stock.code start_date=quant_start_date end_date=quant_end_date exchange_type=predict_type %}">预测</a>
                                                </p>
                                                <p id="{% url 'stocksys:buy_stock' %}"
                                                   onclick="buy_stock({{ stock_info.stock.exchange_address.id }},'{{ stock_info.stock.code }}','{{ stock_info.stock.stock_name }}','{{ stock_info.current_price | floatformat:3 }}')"
                                                   rel="tooltip" title="买入"
                                                   class="btn btn-success btn-simple btn-xs">
                                                    <i>买入</i>
                                                </p>
                                                <p id="{% url 'stocksys:sell_stock' %}"
                                                   onclick="sell_stock({{ stock_info.stock.exchange_address.id }},'{{ stock_info.stock.stock_code }}','{{ stock_info.stock.stock_name }}','{{ stock_info.current_price | floatformat:3 }}')"
                                                   rel="tooltip" title="卖出"
                                                   class="btn btn-error btn-simple btn-xs">
                                                    <i>卖出</i>
                                                </p>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block pagescript %}
    <script type="text/javascript">
        $(document).ready(function () {
            $('#datatables').DataTable({
                "pagingType": "full_numbers",
                "lengthMenu": [
                    [10, 25, 50, -1],
                    [10, 25, 50, "All"]
                ],
                responsive: true,
                language: {
                    search: "_INPUT_",
                    searchPlaceholder: "检索关键字",
                }

            });
            var table = $('#datatables').DataTable();

        });
    </script>
{% endblock %}

